package com.api.gbhg.lbj.action;

import com.alibaba.fastjson.JSONObject;
import com.api.gbhg.huangwm.dao.ReplaceJingfeiDao;
import com.api.gbhg.lbj.dao.ReimbursementDao;
import com.api.gbhg.zengping.common.CommonDao;
import com.api.gbhg.zengping.dao.PropertyManagementCostDao;
import com.api.gbhg.zengping.web.PropertyManagementAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.DetailTable;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lbj
 * @version 1.0
 * @description: TODO 经费来源&收入额度冻结金额(明细表)
 * @date 2021/4/17 15:52
 */
public class    AccountingFrozenamountAction implements Action {
    private static final Logger logger = LoggerFactory.getLogger("gbhg");
    @Override
    public String execute(RequestInfo requestInfo) {
        logger.info("进入会计节点后冻结金额AccountingFrozenamountAction明细表---");
        ReimbursementDao rei = new ReimbursementDao();
        //请求id
        String requestid = requestInfo.getRequestid();
        //获取表名
        String billTableName = requestInfo.getRequestManager().getBillTableName();
        //得到明细表名
        DetailTable[] detailTables = CommonDao.getDetailTables(requestInfo);
        RecordSet rs = CommonDao.selectByRequestId(new RecordSet(), billTableName, requestid);
        rs.next();
        String mainid = rs.getString("id");
        RecordSet recordSet = CommonDao.selectByDetailTable(detailTables[0].getTableDBName(), mainid);
        Map<String, String>[] list = new HashMap[recordSet.getArray().size()];
        int i = 0;
        while (recordSet.next()) {
            logger.info("查询数据结果"+i);
            list[i] = new HashMap<>();
            String jbbm = rs.getString("jbbm");
            String dw = recordSet.getString("szdlhsdw");
            String ysxm = recordSet.getString("ysxm");
            String tybxje = recordSet.getString("tybxje");
            String sqbxje = recordSet.getString("sqbxje");
            String bmjjzcfl = recordSet.getString("bmjjzcfl");
            String zcqd = recordSet.getString("zcqd");
            String ysly = recordSet.getString("ysly");
            String fzysrfl = recordSet.getString("fzysrfl");
            String xzkmdm = recordSet.getString("xzkmdm");
            String nd = recordSet.getString("nd");
            String zcdfczbkxm = recordSet.getString("zcdfczbkxm");
            String cxje = recordSet.getString("cxje");
            logger.info("经办部门:"+jbbm+"预算项目:" + ysxm +"单位:"+dw+",冲销金额:"+cxje+ "同意报销金额:" + tybxje + "部门经济支出分类:" + bmjjzcfl + "支出渠道:" + zcqd + "预算来源:" + ysly + "非中央收入分类:" + fzysrfl);
            list[i].put("lcjlid", ysxm);//预算项目
            list[i].put("tybxje", tybxje);//同意报销金额
            list[i].put("bmjjzcfl", bmjjzcfl);//部门经济支出分类
            list[i].put("zcqd", zcqd);//支出渠道
            list[i].put("ysly", ysly);//预算来源
            list[i].put("fzysrfl", fzysrfl);//非中央收入分类
            list[i].put("xzkmdm", xzkmdm);//选择科目代码
            list[i].put("nd", nd);//选择科目代码
            list[i].put("zcdfczbkxm", zcdfczbkxm);//支出地方财政拨款项目
            list[i].put("cxje", cxje);//冲销金额
            list[i].put("money", sqbxje);//申请报销金额
            list[i].put("dw", dw);//单位
            list[i].put("jbbm", jbbm);//经办部门
            i++;
        }
        RecordSetTrans rst = new RecordSetTrans();
        rst.setAutoCommit(false);
        boolean b2 = rei.jingfeilaiyuandonjie(rst,list, false);//冻结扣减
        if (!b2) {
            rst.rollback();
            //删除报销记录
            rs.execute("DELETE FROM uf_bxjl_kj WHERE lcid="+requestid);
            rs.execute("DELETE FROM uf_bxjl_kj_dt1 WHERE lcid="+requestid);
            rs.execute("DELETE FROM uf_bxjl_kj_dt2 WHERE lcid="+requestid);
            ReplaceJingfeiDao replaceJingfei = new ReplaceJingfeiDao();
            RecordSet recordSet2 = CommonDao.selectByDetailTable(detailTables[0].getTableDBName(), mainid);
            while (recordSet2.next()){
                String ysxmid = recordSet.getString("ysxm");
                Map<String,Object> result = replaceJingfei.replace(ysxmid);
                logger.info(JSONObject.toJSONString(result));
                boolean jingfei = (boolean) result.get("result1");
                boolean jingfeilaiyuan = (boolean) result.get("result2");
                logger.info("异常时更新jingfei信息 jingfei="+jingfei+" jingfeilaiyuan="+jingfeilaiyuan);
            }

            logger.info("=====================冻结错误=======================");
            logger.info("结束会计节点后冻结金额AccountingFrozenamountAction明细表---");
            requestInfo.getRequestManager().setMessagecontent("冻结失败");
            requestInfo.getRequestManager().setMessageid("1");
            return FAILURE_AND_CONTINUE;
        }
        logger.info("结束会计节点后冻结金额AccountingFrozenamountAction明细表---");
        rst.commit();
        return Action.SUCCESS;
    }
}
